<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-12-4
 * Time: 下午8:11
 */
class ResourceAction extends  Action{
    //显示资源:
    public function admin_show_resource(){
    	$type=intval($_GET["type"]);
        $category=intval($_GET["category"]);
        $where["is_del"]=0;
    	if($type>0){
    		$where["type"]=$type;
    	}
        if($category>0){
            $where["category"]=$category;
        }
    	$data=M("resource")->where($where)->findPage(30);
    	foreach ($data["data"] as $key => $value) {
			$data["data"][$key]["img_url"]=getAttachUrlByAttachId($value["resource_img"]);
		}
    	$this->assign("data",$data);
    	$this->assign("type",$type);
        $this->assign("category",$category);
        $this->display("admin_show_resource");
    }
    //资源详情
    public function view(){
    	$id=$_GET["id"];
    	$data=M("resource")->where("id=".$id)->find();
    	$data["img_url"]=getAttachUrlByAttachId($data["resource_img"]);
    	$data["file_url"]=getAttachUrlByAttachId($data["attach_id"]);
        $resource_id=explode("/",$data["resource_type"]);
        $resource_type=M("resource_type")->where("id=".$resource_id[0])->find();
        $data["type_name"]=$resource_type["name"];
    	$resource_type=M("resource_type")->findALL();
        $tj_data=M("resource")->where("is_del=0 and is_tj=1 and id!=".$id)->order("ctime desc")->field("id,resource_img,resource_name")->limit("4")->select();
        foreach ($tj_data as $key => $value) {
            $tj_data[$key]["img_url"]=getAttachUrlByAttachId($value["resource_img"]);
        }
		$this->assign("tj_data",$tj_data);
        $this->assign("resource_type",$resource_type);
    	$this->assign("data",$data);
    	$this->display();
    }
    //跳转至上传页面
    public function show_upload_page(){
        $id=intval($_GET["id"]);
        if($id){
            $data=M("resource")->where("id=".$id)->find();
            $data["img_url"]=getAttachUrlByAttachId($data["resource_img"]);
            $data["file_url"]=getAttachUrlByAttachId($data["attach_id"]);
            $resource_type=explode(",", $data["resource_type"]);
            $type_name="";
            foreach ($resource_type as $key => $value) {
               $resource=explode("/",$value);
               $name="";
               foreach ($resource as $k => $v) {
                   $resourceType=M("resource_type")->where("id=".$v)->find();
                   if($data["type"]==2){
                        if($key==0){
                            if($k==0){
                                $name.=$resourceType["name"]."&nbsp;&nbsp;";
                            }
                            if($k==1){
                                $name.=$resourceType["name"]."-";
                            }
                            if($k==2){
                                $name.=$resourceType["name"]."&nbsp;&nbsp;";
                            }
                        }else{
                            if($k>=0){
                                if($k==1){
                                    $name=$resourceType["name"];
                                }else{
                                    $name.="-".$resourceType["name"];
                                }
                            }
                        }
                    }else{
                        if($k==0){
                            $name=$resourceType["name"];
                        }else{
                            $name.="-".$resourceType["name"];
                        }
                    }
                }
                $type_name.=$name."  ";
            }
            $data["type_name"]=$type_name;
            $this->assign("data",$data);
        }
    	$resource_type=M("resource_type")->where("parent=0 and type=1")->findALL();
		$this->assign("resource_type",$resource_type);
        $this->display("admin_upload_resource");
    }
    //上传视频
    public function doAddAttach(){
        $img=strtolower($_FILES["file"]["name"]);  
        $d['attach_type'] = 'Resource_file';
        $info = model('Attach')->upload($d,$d);
        print_r($info["info"][0]["attach_id"]);
    }
    public function doAddImg(){
    	$file=$_FILES['cover'];
		$d['attach_type'] = 'Resource_image';
        $info = model('Attach')->upload($d,$d);
        $attachInfo = model('Attach')->where('attach_id='.$info["info"][0]["attach_id"])->find();
        $url=UPLOAD_URL.'/'.$attachInfo['save_path'].$attachInfo['save_name'];
        $id=$info["info"][0]["attach_id"];
		echo "<script>parent.stopSend('$url','$id')</script>";
    }
    //上传教师资源
    public function doAddResource(){
        $id=intval($_POST["id"]);
    	$data=array(
    		'type'=>intval($_POST['type']),
    		'category'=>intval($_POST['category']),
    		'resource_img'=>intval($_POST['resource_img']),
    		'resource_name'=>$_POST['resource_name'],
    		'resource_info'=>$_POST['resource_info'],
    		'resource_teacher'=>$_POST['resource_teacher'],
    		'attach_id'=>intval($_POST['attach_id']),
    		'resource_type'=>$_POST['resource_type'],
    		'is_tj'=>intval($_POST['is_tj']),
    		'admin_id'=>$this->mid,
    		'ctime'=>time()
    	);
        $res=$id>0 ? M("resource")->where("id=".$id)->save($data) : M("resource")->add($data);
        if($res){
            echo $res;
            exit;
        }else{
            echo "操作失败，请重试！";
            exit;
        }
    }
    public function getResourceType(){
        $parent=$_GET["parent"];
        $type=$_GET["type"];
        $where=array(
            'parent'=>$parent,
            'type'=>$type
        );
        $data = M("resource_type")->where($where)->findAll();
        foreach ($data as $key => $value) {
            if($value["id"]==31 || $value["id"]==46){
                $data[$key]['level']=D("resource_teacher")->field("id,name")->select();
            }else{
                $data[$key]['level']=M("resource_type")->where("parent=".$value["id"])->findAll();
            }
        }
        echo json_encode($data);
        exit();
    }
    public function teacher(){
        $data=M("resource_teacher")->where("is_del=0")->select();
        $this->assign("data",$data);
        $this->display();
    }
    public function doUpdateTeacher(){
        $data['name']=$_POST["name"];
        $id=intval($_POST["id"]);
        $res= $id>0 ? M("resource_teacher")->where("id=".$id)->save($data) : M("resource_teacher")->add($data);
        if($res){
            echo $res;
            exit;
        }else{
            echo "操作失败，请重试！";
            exit;
        }
    }
    public function DelTeacher(){
        $where['id']=$_POST["id"];
        $res=M("resource_teacher")->where($where)->save(array('is_del'=>1));
        if($res){
            echo $res;
            exit;
        }else{
            echo "删除失败，请重试！";
            exit;
        }
    }
    public function delete_Resource(){
        $where['id']=$_POST["id"];
        $res=M("resource")->where($where)->save(array('is_del'=>1));
        if($res){
            echo $res;
            exit;
        }else{
            echo M("resource")->getLastSql();
            exit;
        }
    }
}